// 公共数据
import { defineStore } from 'pinia'
import { setSessionStoragePinia } from '@/utils/localstorage'
import {
  getGameTotal,
  getCurrencyTotal,
  getOperatorTotal,
  getAgentTotal,
  getAgentCurrencyTotal,
  getLanguageList
} from '@/api/common'

// type商户汇总
interface DataType {
  value: number
  label: string,
  type?:number
  remark?:string
}
export const useStore = defineStore({
  id: 'useStore',
  state: () => {
    return {
      path: '', // 页面路径
      loading: false, // 页面加载
      offset: 0, // 时区偏移量
      breadArray: [] as any[], // 顶部菜单导航栏
      gameArray: [] as Array<DataType>, // 游戏汇总
      is_general_agent: false, // 判断是否是总代登录 判断是否显示商户类别
      currentArray: [] as Array<DataType>, // 货币汇总
      operatorArray: [] as Array<DataType>, // 运营商汇总
      agentArray: [] as Array<DataType>, // 商户汇总
      agentCurrency: [] as Array<DataType>, // 商户货币汇总 (回显商户货币+图标)
      langList: [] as Array<DataType>, // 语言列表
    }
  },
  getters: {},
  actions: {
    // 设置路由路径
    setLocalPath(path: string) {
      this.path = path
    },
    // 设置页面加载
    setPageLoading(loading: boolean) {
      this.loading = loading
    },
    // 设置时区偏移量
    setOffset(offset: number) {
      this.offset = offset
    },
    // 设置导航菜单栏
    setLocalBread(breadArray: any) {
      this.breadArray = breadArray
    },
    // 获取游戏汇总
    async getGame() {
      const res = await getGameTotal()
      const { data } = res
      this.gameArray = data?.sort(function(a, b){
        return a.value - b.value
      })
    },
    // 获取货币汇总
    async getCurrent() {
      const res = await getCurrencyTotal()
      const { data } = res
      this.currentArray = data
    },
    // 获取总代汇总
    async getOperator() {
      const res = await getOperatorTotal()
      const { data } = res
      this.operatorArray = data
    },
    // 获取商户汇总
    async getAgent() {
      const res = await getAgentTotal()
      const { data } = res
      this.agentArray = data
    },
    // 获取商户货币汇总
    async getAgentCurrency() {
      const res = await getAgentCurrencyTotal()
      const { data } = res
      this.agentCurrency = data
    },
    // 语言列表
    async getLanguageList() {
      if (this.langList.length) {
        return
      }
      const res = await getLanguageList()
      const { data } = res
      this.langList = data
    }
  }
})

// 持久化
setSessionStoragePinia(useStore, 'useStore')
